<?php

namespace Managers;

class Reviews extends BaseManager
{
       protected $__table = '\Catalog\Reviews';




     public  function  getList()

    {

        $res =  $this->_em->createQueryBuilder()
                ->select("s" )
                ->from($this->__table, "s")
                 ->orderBy('s.id',' DESC ')
                ->getQuery()
                ->getArrayResult();


         foreach ($res as $key => $value) {

                     $suma = $this->_em->createQueryBuilder()
                                ->select("sum(s.price) as suma" )
                                ->from($this->__table, "s")
                                ->getQuery()
                                ->getArrayResult();
                  $res[$key]['suma']=$suma;
         }
        return $res;






    }
    public  function  addComment($nick,$comment,$price,$quality,$respons,$punctuality,$professionalism)
    {

        
        $review = new \Catalog\Reviews();
        $review->setNick($nick);
        $review->setComment($comment);
        $review->setPrice($price);
        $review->setQuality($quality);
        $review->setResponsiveness($respons);
        $review->setPunctuality($punctuality);
        $review->setProfessionalism($professionalism);
        $this->_em->persist($review);
        $this->_em->flush();
        return $review->getId();


    }

   function resizer($name, $des_a_img_url, $file, $d_x)
    {
        if (!extension_loaded('imagick')) {
            throw new \Exception('Image magick php is not installed.');
        }
        try {
            $image = new \Imagick($file);
            $image->thumbnailImage($d_x, null);
            $fd = fopen($des_a_img_url, 'w');
            $image->writeImageFile($fd);
            fclose($fd);
            unlink($file);
        } catch (\ImagickException $e) {
            throw new \Exception('Unsupported file format.');
        }
    }

     public function update($id,$nick,$comment,$price,$quality,$respons,$punctuality,$professionalism)
    {
        $review = $this->_em->find($this->__table, $id);
        if ($review) {
        
            $review->setNick($nick);
            $review->setComment($comment);
            $review->setPrice($price);
            $review->setQuality($quality);
            $review->setResponsiveness($respons);
            $review->setPunctuality($punctuality);
            $review->setProfessionalism($professionalism);
            $this->_em->persist($review);
            $this->_em->flush();

            return $review;
        }
        return false;
    }
    



}
